package ch13;

import java.util.ArrayList;

public class CalculationMonth extends Option {

	Keypad keypad;
	// irrelevant stringarray for menu-visualization
	private ArrayList<String> stringArray = new ArrayList<String>();

	// BalanceInquiry constructor
	public CalculationMonth(int userAccountNumber, Screen atmScreen,
			BankDatabase atmBankDatabase, Keypad atmKeypad) {
		super(userAccountNumber, atmScreen, atmBankDatabase);
		keypad = new Keypad();
	} // end BalanceInquiry constructor

	// performs the transaction
	@Override
	public void execute() {

		int month = 0;

		stringArray.clear(); // clear the screen box
		stringArray.add("type in Loan type:  ");
		screen.setKasse(stringArray, 10); // set the size of the screen box
		String loanType = keypad.getInputS();

		stringArray.clear(); // clear the screen box
		stringArray.add("Type loan amount: ");
		screen.setKasse(stringArray, 10); // set the size of the screen box
		double loanAmount = keypad.getInput();

		stringArray.clear(); // clear the screen box
		stringArray.add("Type interest:  ");
		screen.setKasse(stringArray, 10); // set the size of the screen box
		double interest = keypad.getInput();

		stringArray.clear(); // clear the screen box
		stringArray.add("Type monthlyRate: ");
		screen.setKasse(stringArray, 10); // set the size of the screen box
		double monthlyRate = keypad.getInput();

		double total = 0;
		double loanAmountW = loanAmount;

		// if monthlyrate is to low 
		if ((loanAmountW * Math.pow((1 + (interest / 100)), 1)) > (loanAmount + monthlyRate)){
			stringArray.clear();
			stringArray.add("Monthly payment to low");
			screen.setKasse(stringArray, 10);}
		else {
			// while method for getting how many months will go before loan is paid
			while (loanAmountW > 0) {

				loanAmountW = (loanAmountW * Math
						.pow((1 + (interest / 100)), 1));

				if (loanAmountW < monthlyRate)
					monthlyRate = loanAmountW;

				loanAmountW = loanAmountW - monthlyRate;
				total += monthlyRate;

				month++;
			} // end while

			int totalInt = (int) total;
			stringArray.clear();
			stringArray.add("Requested loan type: " + loanType);
			stringArray.add("The rent is: " + interest);
			stringArray.add("The loan is paid back in:  " + month + " months.");
			stringArray.add("The total price after   " + month
					+ " months will be: " + totalInt);
			screen.setKasse(stringArray, 10); // set the size of the screen box

		}// end else statement
	}// end method execute
}// end class Calculation Month